const express = require('express')
const config = require('./config')
const axios = require('axios');

const app = express();

const router = express.Router()
router.get('/getDiscList2',  (req, res)=> {
  let url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
  axios.get(url, {
    headers: {
      referer: 'https://c.y.qq.com/',
      host: 'c.y.qq.com'
    },
    params: req.query
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
  })
});
router.get('/getLyric',  (req, res)=> {
  let url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg'
  axios.get(url, {
    headers: {
      referer: 'https://c.y.qq.com/',
      host: 'c.y.qq.com'
    },
    params: req.query
  }).then((response) => {
    // 数据处理，类似jsonp的对象
    let ret = response.data
    if(typeof ret === 'string'){
      let reg = /^\w+\(({[^()]+})\)$/
      let matches = ret.match(reg)
      if(matches){
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret)
  }).catch((e) => {
    console.log(e)
  })
});

app.use('/api', router);

app.use(express.static('./dist'))

const port = process.env.PORT || config.build.port
module.exports = app.listen(port, function(err) {
  if (err) {
    console.log(err)
    return
  }
  console.log('Your application is running here: http://localhost:' + port + '\n')
})
